﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Leo.Common
{
    public class FuncResult
    {
        public const string _FAIL_CODE = "0500";

        public const string _SUCCESS_CODE = "0000";

        public const string _SUCCESS_MESSAGE = "操作成功";

        public string Code { get; set; }

        public string Message { get; set; }


        public static FuncResult<T> Success<T>(T content, string message = null)
        {
            return new FuncResult<T>
            {
                Data = content,
                Code = "0000",
                Message = (message ?? "操作成功")
            };
        }


        public static FuncResult Success(string message = null)
        {
            return new FuncResult
            {
                Code = "0000",
                Message = (message ?? "操作成功")
            };
        }


        public static FuncResult<T> Fail<T>(T data, string message, string code = "0500")
        {
            return new FuncResult<T>
            {
                Data = data,
                Message = message,
                Code = code
            };
        }


        public static FuncResult<T> Fail<T>(string message, string code = "0500")
        {
            return FuncResult.Fail<T>(default(T), message, code);
        }


        public static FuncResult Fail(string message, string code = "0500")
        {
            return new FuncResult
            {
                Message = message,
                Code = code
            };
        }


        public static implicit operator bool(FuncResult result)
        {
            return result != null && result.Code == "0000";
        }


        public static FuncResultPagination<T> Success<T>(IEnumerable<T> array, int total)
        {
            return new FuncResultPagination<T>
            {
                Code = "0000",
                Message = "操作成功",
                Data = new PaginationResult<T>
                {
                    Total = total,
                    Array = (array ?? new List<T>())
                }
            };
        }


        public static FuncResultPagination<T> FailList<T>(string message, string code = "0500")
        {
            return new FuncResultPagination<T>
            {
                Code = code,
                Message = message
            };
        }



    }
}
